package cn.wp.ewallet.wmps.web.controller;

import cn.wp.common.core.Wp;
import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.service.MoneyChangeService;
import cn.wp.ewallet.wallet.service.WalletInfoService;
import cn.wp.ewallet.wmps.extension.constant.EwalletWmpsConstant;
import cn.wp.ewallet.wmps.model.dto.FinaProdCountDto;
import cn.wp.ewallet.wmps.model.dto.FinaProdInfoDto;
import cn.wp.ewallet.wmps.model.dto.ProdOrderParamDto;
import cn.wp.ewallet.wmps.model.dto.TtyStatInfoDto;
import cn.wp.ewallet.wmps.model.dto.*;
import cn.wp.ewallet.wmps.service.EarnDetailLogService;
import cn.wp.ewallet.wmps.service.FinaProdInfoService;
import cn.wp.ewallet.wmps.service.ProdOrderRelService;
import cn.wp.ewallet.wmps.service.TtySSettlementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping(Wp.WebContext + "/ewallet/wmps/finaProd")
@Api(tags = "理财产品")
public class FinaProdInfoController {

    @Autowired
    private FinaProdInfoService finaProdInfoService;

    @Autowired
    private ProdOrderRelService prodOrderRelService;

    @Autowired
    private EarnDetailLogService earnDetailLogService;

    @Autowired
    private MoneyChangeService moneyChangeService;

    @Autowired
    private WalletInfoService walletInfoService;

    @Autowired
    private TtySSettlementService ttySSettlementService;

    @GetMapping("/getAllFinaProdInfoList")
    @ApiOperation("获取所有理财产品信息")
    public WpResult getAllFinaProdInfoList(String finaProdName, WpPage wpPage) {
        return new WpResult().success(finaProdInfoService.getFinaProdInfoList(finaProdName, wpPage, "all"));
    }

    @GetMapping("/getUpperFinaProdInfoList")
    @ApiOperation("获取上架理财产品信息")
    public WpResult getUpperFinaProdInfoList(int page, int limit) {
        return new WpResult().success(finaProdInfoService.getFinaProdInfoList(null, new WpPage(page, limit), "upper"));
    }

    @PostMapping("/saveFinaProdInfo")
    @ApiOperation("创建理财产品")
    public WpResult saveFinaProdInfo(FinaProdInfoDto finaProdInfoDto) {
        finaProdInfoService.saveFinaProdInfo(finaProdInfoDto);
        return new WpResult().success();
    }

    @PostMapping("/modifyFinaProdInfo")
    @ApiOperation("编辑理财产品")
    public WpResult modifyFinaProdInfo(FinaProdInfoDto finaProdInfoDto) {
        return new WpResult().success(finaProdInfoService.modifyFinaProdInfo(finaProdInfoDto));
    }

    @PostMapping("/upperFinaProdInfo")
    @ApiOperation("上架理财产品")
    public WpResult upperFinaProdInfo(String finaProdId) {
        finaProdInfoService.upperFinaProdInfo(finaProdId);
        return new WpResult().success();
    }

    @PostMapping("/lowerFinaProdInfo")
    @ApiOperation("下架理财产品")
    public WpResult lowerFinaProdInfo(String finaProdId) {
        finaProdInfoService.lowerFinaProdInfo(finaProdId);
        return new WpResult().success();
    }

    @PostMapping("/purchaseFinaProd")
    @ApiOperation("购买理财产品")
    public WpResult purchaseFinaProd(String finaProdId, BigDecimal amount, String isAutoRenewal, String moneyTypeId, String transPwd) {
        //校验交易密码
        if (!walletInfoService.checkTransPwd(transPwd)) {
            return new WpResult().failure(EwalletWalletConstant.CHECK_TRANSPWD_FAILED_CODE, "交易密码校验失败");
        }

        return prodOrderRelService.purchaseFinaProd(finaProdId, amount, isAutoRenewal, moneyTypeId);
    }

    @GetMapping("/getEarnDailyLogList")
    @ApiOperation("理财收益明细查询")
    public WpResult getEarnDailyLogList(int month, int page, int limit, String earnStatus) {
        return new WpResult().success(earnDetailLogService.getEarnDetailLogList(month, new WpPage(page, limit), earnStatus));
    }

    @GetMapping("/getFixFinaAmount")
    @ApiOperation("定期理财总额信息查询")
    public WpResult getFixFinaAmount() {
        return new WpResult().success(finaProdInfoService.getFixFinaAmountInfo());
    }

    @GetMapping("/getHoldFixFinaInfo")
    @ApiOperation("持有定期理财产品订单信息查询")
    public WpResult getHoldFixFinaInfo() {
        return new WpResult().success(finaProdInfoService.getFixFinaInfo(EwalletWmpsConstant.ORDER_STATUS_BEARING, EwalletWmpsConstant.ORDER_STATUS_WAITING));
    }

    @GetMapping("/getFixFinaProdInfo")
    @ApiOperation("已结束定期理财产品订单信息查询")
    public WpResult getFinishedFixFinaInfo() {
        return new WpResult().success(finaProdInfoService.getFixFinaInfo(EwalletWmpsConstant.ORDER_STATUS_FINISH, EwalletWmpsConstant.ORDER_STATUS_STOP));
    }

    @GetMapping("/getProdDetailInfoByProdOrderRelId")
    @ApiOperation("查询理财订单详情")
    public WpResult getProdDetailInfoByProdOrderRelId(String prodOrderRelId) {
        return new WpResult().success(prodOrderRelService.getProdDetailInfoByOrderRelId(prodOrderRelId));
    }


    @GetMapping("/getPersonalTotal")
    @ApiOperation("查询个人理财总额")
    public WpResult getPersonalTotal() {
        return new WpResult().success(prodOrderRelService.getPersonalTotal());
    }

    /**
     * status的值为Y/N
     *
     * @param prodOrderId
     * @param status
     * @return
     */
    @PostMapping("/modifyRenewalStatus")
    @ApiOperation("修改订单赎回方式")
    public WpResult modifyRenewalStatus(String prodOrderId, String status, String transPwd) {
        //校验交易密码
        if (!walletInfoService.checkTransPwd(transPwd)) {
            return new WpResult().failure(EwalletWalletConstant.CHECK_TRANSPWD_FAILED_CODE, "交易密码校验失败");
        }
        prodOrderRelService.modifyRenewalStatus(prodOrderId, status);
        return new WpResult().success();

    }

    @PostMapping("/earlRedemption")
    @ApiOperation("定期理财产品提前赎回")
    public WpResult earlRedemption(String prodOrderId, String finaProdId, String transPwd) {
        //校验交易密码
        if (!walletInfoService.checkTransPwd(transPwd)) {
            return new WpResult().failure(EwalletWalletConstant.CHECK_TRANSPWD_FAILED_CODE, "交易密码校验失败");
        }
        return moneyChangeService.redemptionMoney(prodOrderId, finaProdId);
    }

    @GetMapping("/getTtyStatInfo")
    @ApiOperation("个人天天盈理财信息查询")
    public WpResult getTtyStatInfo() {

        TtyStatInfoDto ttyStatInfoDto = prodOrderRelService.getTtyStatInfo();
        return new WpResult().success(ttyStatInfoDto);
    }

    @PostMapping("dayDayMakeMoneyTransferInto")
    @ApiOperation("天天盈转入")
    public WpResult dayDayMakeMoneyTransferInto(ProdOrderParamDto prodOrderParamDto) {
        //校验交易密码
        if (!walletInfoService.checkTransPwd(prodOrderParamDto.getTransPwd())) {
            return new WpResult().failure(EwalletWalletConstant.CHECK_TRANSPWD_FAILED_CODE, "交易密码校验失败");
        }
        return moneyChangeService.tiantianyingTransferInto(prodOrderParamDto.getTurnOutMoney());
    }

    @PostMapping("dayDayMakeMoneyTransferOut")
    @ApiOperation("天天盈转出")
    public WpResult dayDayMakeMoneyTransferOut(ProdOrderParamDto prodOrderParamDto) {
        //校验交易密码
        if (!walletInfoService.checkTransPwd(prodOrderParamDto.getTransPwd())) {
            return new WpResult().failure(EwalletWalletConstant.CHECK_TRANSPWD_FAILED_CODE, "交易密码校验失败");
        }
        return moneyChangeService.tiantianyingTransferOut(prodOrderParamDto.getTurnOutMoney());
    }

    @PostMapping("modifyYieldRate")
    @ApiOperation("天天盈年化率修改")
    public WpResult modifyYieldRate(BigDecimal yieldRate, String finaProdDesc) {
        finaProdInfoService.modifyYieldRate(yieldRate, finaProdDesc);
        return new WpResult().success();
    }

    @GetMapping("getTtyParams")
    @ApiOperation("获取天天盈配置参数")
    public WpResult getTtyParams() {
        return finaProdInfoService.getTtyParams();
    }


    @GetMapping("getTtyCountInfo")
    @ApiOperation("获取天天盈理财概览")
    public WpResult getTtyCountInfo() {
        return ttySSettlementService.getTtyCountInfo();
    }

    @GetMapping("/getFinaProdInfoDtoWpPage")
    @ApiOperation("理财产品列表查询(带条件或者不带条件)")
    public WpResult getFinaProdInfoDtoWpPage(FinaParmDto finaParmDto, WpPage wpPage) {
        WpPage<FinaProdAddressDto> finaProdInfoListByParam = finaProdInfoService.getFinaProdInfoListByParam(finaParmDto, wpPage);
        return new WpResult().success(finaProdInfoListByParam);
    }
    
    @GetMapping("/getFixFinaProdCountDtos")
    @ApiOperation("定期理财概览查询")
    public WpResult getFixFinaProdCountDtos(WpPage wpPage) {
        return new WpResult().success(finaProdInfoService.getFixFinaProdCountDtoList(wpPage));
    }



    @GetMapping("/getgetFixFinaProdCountPercent")
    @ApiOperation("定期理财百分比查询")
    public WpResult getgetFixFinaProdCountPercent() {
        return finaProdInfoService.getgetFixFinaProdCountPercent();
    }


}
